home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Snippets / FakeAlert / FakeAlert.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-31  |  5.7 KB  |  218 lines  |  [TEXT/KAHL]

  1. #include    <Dialogs.h>
  2. #include    <Types.h>
  3. #include    "FakeAlert.h"
  4. #include    "WindUtils.h"
  5.  
  6. //This code was originally found in a TransSkel release
  7. //This version has been polished a bit by James Chandler Jr., CIS 71333,2651
  8. //It draws an alert without requiring an ALRT or DITL resource
  9.  
  10. //examples---
  11.  
  12. //FakeAlert ("\pMIDI Manager already open!", "\p", "\p", "\p", 1, 1, "\pOK",  "\p", "\p");
  13. //draws alert string 1, uses 1 button "OK", with button 1 the default
  14.  
  15. //rc = FakeAlert ("\pThe file \"", SetList[CurrentSetListItem],"\p\" could not be found.  Want to find it?.","\p", 2, 1, "\pFind",  "\pCancel", "\p");
  16. //    if (rc == 1)
  17. //    {
  18. //    }
  19. //else
  20. //    {
  21. //    }
  22. // Prints--  The file "WipeOut" could not be found.  Want to find it?
  23. // assuming SetList[CurrentSetListItem] is an unsigned char string pointer
  24. // then it puts up two buttons, "Find" and "Cancel", with "Find" as the default.
  25.  
  26. //This version will properly respond to return key as equivalent to clicking default button
  27.  
  28. static short DefaultButton;
  29.  
  30. static pascal Boolean myFilterProc(DialogPtr theDialog, EventRecord *theEvent, short *itemHit);    //prototype
  31.  
  32. /*
  33.     In-memory item list for dialog with four items:
  34.  
  35.     1    "^0^1^2^3" (static text)
  36.     2    Button 1
  37.     3    Button 2
  38.     4    Button 3
  39.  
  40.     The caller of FakeAlert passes the four strings that are to be
  41.     substituted into the first item, the number of buttons that
  42.     should be used, and the titles to put into each button.
  43.     A copy of the item list is hacked to use the right number of
  44.     buttons.
  45.  
  46.     Thanks to Erik Kilk and Jason Haines.  Some of the stuff to do
  47.     this is modified from code they wrote.
  48. */
  49.  
  50.  
  51. static short    itemList [] =
  52. {
  53.     3,                    /* max number of items - 1 */
  54.  
  55. /*
  56.     statText item
  57. */
  58.     0, 0,                /* reserve a long for item handle */
  59.     10, 27, 61, 225,    /* display rectangle */
  60.     ((8 + 128) << 8) | 8,    /* 8 + 128 = statText (disabled), title 8 bytes long */
  61.     '^0', '^1',        /* ^0^1^2^3 */
  62.     '^2', '^3',
  63.  
  64. /*
  65.     first button
  66. */
  67.  
  68.     0, 0,                /* reserve a long for item handle */
  69.     102, 140, 120, 210,    /* display rectangle */
  70.     (4 << 8) | 0,        /* 4 = pushButton, title is 0 bytes long*/
  71.  
  72. /*
  73.     second button
  74. */
  75.  
  76.     0, 0,                /* reserve a long for item handle */
  77.     102, 30, 120, 100,    /* display rectangle */
  78.     (4 << 8) | 0,        /* 4 = pushButton, title is 0 bytes long */
  79.  
  80. /*
  81.     third button
  82. */
  83.  
  84.     0, 0,                /* reserve a long for item handle */
  85.     78, 30, 96, 100,    /* display rectangle */
  86.     (4 << 8) | 0        /* 4 = pushButton, title is 0 bytes long */
  87. };
  88.  
  89.  
  90. /*
  91.     Set dialog button title and draw bold outline if makeBold true.
  92.     This must be done after the window is shown or else the bold
  93.     outline won't show up (which is probably the wrong way to do it).
  94. */
  95.  
  96. static void SetDControl (DialogPtr    theDialog,
  97.                 short    itemNo,
  98.                 StringPtr    title,
  99.                 Boolean    makeBold)
  100. {
  101. Handle    itemHandle;
  102. short    itemType;
  103. Rect        itemRect;
  104. PenState    pState;
  105.  
  106.     GetDItem (theDialog, itemNo, &itemType, &itemHandle, &itemRect);
  107.     SetCTitle ((ControlHandle) itemHandle, title);
  108.     if (makeBold)
  109.     {
  110.         GetPenState (&pState);
  111.         PenNormal ();
  112.         PenSize (3, 3);
  113.         InsetRect (&itemRect, -4, -4);
  114.         FrameRoundRect (&itemRect, 16, 16);
  115.         SetPenState (&pState);
  116.     }
  117. }
  118.  
  119.  
  120. /*
  121.     Fake an alert, using an in-memory window and item list.
  122.     The message to be presented is constructed from the first
  123.     four arguments.  nButtons is the number of buttons to use,
  124.     defButton is the default button, the next three args are
  125.     the titles to put into the buttons.  The return value is
  126.     the button number (1..nButtons).  This must be interpreted
  127.     by the caller, since the buttons may be given arbitrary
  128.     titles.
  129.  
  130.     nButtons should be between 1 and 3, inclusive.
  131.     defButton should be between 1 and nButtons, inclusive.
  132. */
  133.  
  134.  
  135. short FakeAlert (StringPtr    s1,
  136.             StringPtr    s2,
  137.             StringPtr    s3,
  138.             StringPtr    s4,
  139.             short    nButtons,
  140.             short    defButton,
  141.             StringPtr    t1, 
  142.             StringPtr    t2,
  143.             StringPtr    t3)
  144. {
  145. GrafPtr            savePort;
  146. DialogPtr            theDialog;
  147. Handle            iListHandle;
  148. Rect                bounds;
  149. Rect                r;
  150. short            itemHit;
  151. short            itemType;
  152. long                finalTick;
  153. ControlHandle        theControl;
  154. AuxWinHandle        wincolors;
  155.  
  156.     DefaultButton = defButton;
  157.     InitCursor ();
  158.     GetPort (&savePort);
  159.     iListHandle = NewHandle (512L);
  160.     HLock (iListHandle);
  161.     BlockMove (&itemList, *iListHandle, 512L);
  162.     ((short *) *iListHandle)[0] = nButtons;    /* = number items - 1 */
  163.     SetRect (&bounds, 115, 80, 355, 220);
  164.     if (HasColorQD) theDialog = (DialogPtr) NewCDialog (nil, &bounds, "\p", false, dBoxProc, (WindowPtr) -1L, false, 0L, iListHandle);
  165.     //uses public var "HasColorQD" from WindUtils to determine if running on a color mac
  166.     //if you don't want to use WindUtils, you can just use a black'n'white alert window all the time
  167.     //or copy the relevant code from the ColorImplemented() routine in WindUtils.c
  168.     else theDialog = NewDialog (nil, &bounds, "\p", false, dBoxProc, (WindowPtr) -1L, false, 0L, iListHandle);
  169.  
  170.     ParamText (s1, s2, s3, s4);        /* construct message */
  171.  
  172.     SetPort (theDialog);
  173.     TextFont(geneva);
  174.     TextSize(12);
  175.     TextFace(0);
  176.     ShowWindow (theDialog);
  177.  
  178.     switch (nButtons)                /* set button titles */
  179.     {
  180.         case 3:
  181.             SetDControl (theDialog, 4, t3, defButton == 3);
  182.             //fall through...
  183.         case 2:
  184.             SetDControl (theDialog, 3, t2, defButton == 2);
  185.             //fall through...
  186.         case 1:
  187.             SetDControl (theDialog, 2, t1, defButton == 1);
  188.     }
  189.     
  190.     ModalDialog (myFilterProc, &itemHit);
  191.     
  192.     GetDItem(theDialog, itemHit, &itemType, (Handle *) &theControl, &r);
  193.     Delay(5, &finalTick);
  194.     HiliteControl(theControl, 10);
  195.     Delay(10, &finalTick);
  196.     HiliteControl(theControl, 0);
  197.     Delay(5, &finalTick);
  198.  
  199.     HUnlock (iListHandle);
  200.     DisposDialog (theDialog);
  201.     SetPort (savePort);
  202.     return (itemHit - 1);
  203.     // returns 1 -> 3 for button hit.  If key hit, returns default button number
  204. }
  205.  
  206. static pascal Boolean myFilterProc(DialogPtr theDialog, EventRecord *theEvent, short *itemHit)
  207. {
  208.  
  209.     if (theEvent->what == keyDown)
  210.     {
  211.         if ((theEvent->message & charCodeMask) == 0x0D)
  212.         {
  213.             *itemHit = DefaultButton + 1;
  214.             return(true);
  215.         }
  216.     }
  217.     return(false);
  218. }